﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: DecisionTreeNodeBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-12-08 8:26 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;

namespace BOL.Algorithms.Classification
{
    public abstract class DecisionTreeNodeBase<TClass> : IDecisionTreeNode<TClass>
    {
        #region Public properties

        public dynamic Key { get; set; }

        //public IDecisionTreeNode<TClass> Parent { get; private set; } 

        public IList<IDecisionTreeNode<TClass>> Children { get; private set; }

        #endregion

        #region Constructors

        protected DecisionTreeNodeBase(dynamic key, IEnumerable<IDecisionTreeNode<TClass>> children)
        {
            Key = key;
            Children = new List<IDecisionTreeNode<TClass>>(children);
        }

        protected DecisionTreeNodeBase(dynamic key)
        {
            Key = key;
            Children = new List<IDecisionTreeNode<TClass>>();
        }

        #endregion

        #region Public methods

        public void Add(IDecisionTreeNode<TClass> child)
        {
            Children.Add(child);
        }

        public void Remove(IDecisionTreeNode<TClass> child)
        {
            Children.Remove(child);
        }

        #endregion

        #region Object overriden

        public override int GetHashCode()
        {
            return Key.GetHashCode();
        }

        public override string ToString()
        {
            return Key == null ? String.Empty : String.Format("{0}: ", Key);
        }

        public void ToString(ref string prefix, int level)
        {
            prefix += String.Format("{0}{1}\n", String.Concat(Enumerable.Repeat("  ", level)), this);
            level++;
            foreach (var child in Children)
                child.ToString(ref prefix, level);
        }

        #endregion
    }
}